<?php
/**
 * Created by PhpStorm.
 * User: junyv
 * Date: 2016/9/18
 * Time: 11:52
 */

namespace app\manage\controller;
class Weialbum extends Base
{
//前置操作定义
    protected $beforeActionList = [
        // 'second' =>  ['except'=>'hello'],
        'beforeEdit'  =>  ['only'=>'edit'],
    ];
    private $mod;
    private $_cate_mod;
    public function _initialize(){
        parent::_initialize();
        $this->mod = model('weialbum');
        $this->_cate_mod = model('linkmenu');
    }

    public function _search(){
        $map = array();
       ($keyword = input('get.keyword')) && $map['title'] = ['like', '%'.$keyword.'%'];
        $cate_id = input('get.cate_id');
        $status = -1;
        if(isset($_GET['status']) && $_GET['status'] !== ''){
            $status = input('get.status/d');
        }
        $status>=0 && $map['status'] = ['eq',$status];
        $selected_ids = '';
        if ($cate_id) {
            $id_arr = $this->_cate_mod->get_child_ids($cate_id, true);
            $map['cate_id'] = ['in', $id_arr];
            $spid = $this->_cate_mod->where(['id'=>$cate_id])->value('spid');
            $selected_ids = $spid ? $spid . $cate_id : $cate_id;
        }
        $this->assign('search', array(
            'cate_id' => $cate_id,
            'selected_ids' => $selected_ids,
            'status'  => $status,
            'keyword' => $keyword
        ));
        return $map;
    }
    protected function beforeEdit(){
        $id = input('param.id/d');
        $article = $this->mod->field('id,cate_id')->where(['id'=>$id])->find();
        $spid = $this->_cate_mod->where(['id'=>$article['cate_id']])->value('spid');
        if( $spid==0 ){
            $spid = $article['cate_id'];
        }else{
            $spid .= $article['cate_id'];
        }

        $this->assign('selected_ids',$spid);
    }

}